<?php
/**
 * 注册模式----设计模式
 * Created by Jason.
 * User: Administrator
 * Date: 2019/12/7 0007
 * Time: 下午 3:18
 */

namespace app\index\abstracts;


use think\Exception;

abstract class Registry
{
    const LOGGER = 'logger';

    private static $storedValues = [];//定义存储值数组

    //定义合法键名数组,可在此定义用户名唯一性。
    private static $allowedKeys = [
        self::LOGGER,
    ];

    //将对象注册到全局栈
    public static function set(string $key, $value)
    {
        if(!in_array($key,self::$allowedKeys)){
            throw new Exception('不合法的键名！');
        }
        self::$storedValues[$key] = $value;
    }

    //获取全局注册栈里的对象
    public static function get(string $key)
    {
        if(!in_array($key,self::$allowedKeys) || !isset(self::$storedValues[$key])){
            throw new Exception('不合法的键名！');
        }
        return self::$storedValues[$key];
    }

    //移除某个全局注册栈里的对象。
    public static function _unset(string $key){
        if (!isset(self::$storedValues[$key]) || !in_array($key,self::$allowedKeys)){
            throw new Exception('不合法的键名！');
        }
        unset(self::$storedValues[$key]);
    }
}